﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;

namespace TeamPirateForm
{
    public class Crawler
    {
        Form1 form = new Form1();
         //Get Absolute Links.
        public void GetAbsoluteLinks()
        {
            try
            {
                // Using HtmlAgilityPack to load HTML from URL.
                HtmlWeb htmlWeb = new HtmlWeb();

                // Creates an HtmlDocument object from an URL
                HtmlAgilityPack.HtmlDocument document = htmlWeb.Load("Http://" + form.comboBox_Site.Text);

                List<string> hrefTags = new List<string>();

                if (hrefTags != null)
                {
                    foreach (HtmlAgilityPack.HtmlNode node in document.DocumentNode.SelectNodes("//a[@href]"))
                    {
                        //HtmlAgilityPack.HtmlAttribute att = node.Attributes["href"];
                        //if (!att.Value.Equals("") && !att.Value.Contains("javascript"))
                        {
                            // Add each link to the list 'hrefTags'.
                            hrefTags.Add(node.Attributes["href"].Value);
                        }

                    }
                }
                else
                {
                    MessageBox.Show("No Links Found");
                }
                if (hrefTags != null)
                {

                    // View Absolute Links.
                    foreach (var link in from l in hrefTags where l.StartsWith("http://") select l)
                    {
                        form.richTextBox_SelectedTask.Text += "Absolute link: " + "\n" + link + "\n";
                    }

                }
                else
                {
                    form.richTextBox_SelectedTask.Text = "No Absolute Link Values Found";
                }

            }
            catch (Exception)
            {
                MessageBox.Show("No site selected, select one from the dropdown or type in manually.");
            }
        }
    }
}
